package wo.xsys.controller;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import wo.common.entity.WoPage;
import wo.common.exception.WoResultCode;
import wo.common.util.WoUtil;
import wo.xsys.dto.MenuDto;
import wo.xsys.util.XSysConstant;
import wo.xsys.service.MenuService;
import wo.xsys.vo.WoGridData;

/**
 * 菜单对应控制器
 * @author cailei
 */
@RestController
@RequestMapping(value = "/xsys/menu", produces = "application/json")
@SessionAttributes(names = XSysConstant.SESSION_USER)
public class MenuController {
	
	/**
	 * 引入日志打印框架.
	 */
	private final static Logger LOG = LogManager.getLogger(MenuController.class);
	
	/**
	 * 注入MenuService.
	 */
    @Resource // @Autowired
	private MenuService menuService;
	
	/**
     * 查询菜单列表数据
     * @param searchContent 查询内容
     * @param selectedIds 列表中选中的菜单id，如果多个，以逗号隔开
     * @param start
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    public WoGridData<MenuDto> list(String searchContent, String selectedIds, Long start, Long limit) {
    	WoPage<MenuDto> pageData = menuService.getPageData(searchContent, WoUtil.splitIds(selectedIds), start, limit);
        return new WoGridData<MenuDto>(pageData.getRows(), pageData.getResults());
    }

    /**
     * 创建菜单
     * @param m
     * @return
     */
    @RequestMapping("/create")
    public WoResultCode create(MenuDto m) {
        menuService.create(m);
        return WoResultCode.getSuccessCode();
    }

    /**
     * 删除菜单
     * @param selectedIds 列表中选中的菜单id，如果多个，以逗号隔开
     * @return
     */
    @RequestMapping("/delete")
    public WoResultCode delete(String selectedIds) {
        menuService.delete(WoUtil.splitIds(selectedIds));
        return WoResultCode.getSuccessCode();
    }

    /**
     * 修改菜单
     * @param m
     * @return
     */
    @RequestMapping("/update")
    public WoResultCode update(MenuDto m) {
        menuService.update(m);
        return WoResultCode.getSuccessCode();
    }

}
